package com.zzy.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzy.edu.entity.EduComment;
import com.zzy.edu.mapper.EduCommentMapper;
import com.zzy.edu.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author KAZU
 * @since 2023-01-22
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {
	
	@Autowired
	private EduCommentMapper commentMapper;
	
	/**
	 * 评论分页功能
	 * */
	@Override
	public Map<String, Object> getPageList(Page<EduComment> commentPage, String courseId) {
		QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
		if (!StringUtils.isEmpty(courseId)) {
			wrapper.eq("course_id", courseId);
		}
		wrapper.orderByDesc("gmt_create");
		commentMapper.selectPage(commentPage, wrapper);
		List<EduComment> records = commentPage.getRecords();
		long pages = commentPage.getPages();
		long total = commentPage.getTotal();
		long current = commentPage.getCurrent();
		long size = commentPage.getSize();
		boolean hasNext = commentPage.hasNext();
		boolean previous = commentPage.hasPrevious();
		Map<String, Object> map = new HashMap<>();
		map.put("records", records);
		map.put("pages", pages);
		map.put("total", total);
		map.put("current", current);
		map.put("size", size);
		map.put("hasNext", hasNext);
		map.put("previous", previous);
		return map;
	}
}
